<?php
require_once(dirname(__FILE__).'/../../../WildPack/WildPack.php');
$wp = new WildPack();
$wp->using('util');
$wp->using('dataaccess.wpdatex');

require_once(dirname(__FILE__).'/Tester.php');

class TestSimpleDatexS extends WpTestSuite
{
  private $_host = 'localhost';
  private $_user = 'wp';
  private $_password = 'wp';
  private $_db = 'wp';
  private $_table = 'tester';
  
  /**
   * Preparation
   */
  public function prepare()
  {   
    $this->addMessage('Using database host: '.$this->_host);
    $this->addMessage('Using database: '.$this->_db);
    $this->addMessage('Using user: '.$this->_user);
    $this->addMessage('Using database table: '.$this->_table);
    
    $this->addMessage('Connect to database...');
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      die;
    }
    $this->addMessage('... established!');
    
    $this->addMessage('Has database?');
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('Database not found. Try creating...');      
      if (!mysql_query('CREATE DATABASE '.$this->_db)) {
        $this->addMessage('... not successfull. Test stopped.');
        die;
      } else {
        $this->addMessage('... successfull.');
        mysql_select_db($this->_db, $c);    
      }
    } else {
      $this->addMessage('Database found'); 
    }
    
    $r = mysql_query('SHOW TABLES');
    $found = false;
    while ($row = mysql_fetch_array($r)) {
      if ($row[0] === $this->_table) {
        $found=true; break;
      }
    }
    mysql_free_result($result);
    
    if (!$found) {
      if (!mysql_query('CREATE TABLE  `'.$this->_table.'` ('.
        'id int(11) NOT NULL AUTO_INCREMENT,'.
        'field1 varchar(255) NOT NULL,'.
        'field2 double NULL,'.
        'field3 text NULL,'.
        'PRIMARY KEY (id)'.
        ') ENGINE=MyISAM DEFAULT CHARSET=latin1'
      )) {
        $this->addMessage('Table not found. Try to create ... failed. Test stopped.');
        die;
      } else {
        $this->addMessage('Table not found. Try to create ... successfull.');
      }
    } else {
      $this->addMessage('Table found.');
    }
    
    if (!mysql_query('TRUNCATE '.$this->_table)) {
      $this->addMessage('Trying to empty the table ... failed. Test stopped.');
      die;
    }
    
    $this->addMessage('Trying to empty the table ... successfull.');
    return true;
  }
  
  /**
   * Test
   */
  public function test()
  {
    /* Single requests */
    $l = new WpMemLog();
    
    $c = new WpConnectMySQL(array(
      'location' => $this->_host.'/'.$this->_db,
      'user' => $this->_user,
      'password' => $this->_password,
      'log' => $l
    ));
    
    $this->addMessage('Constructor actions');
    $o = new Tester($c, $l, 'new', array(
      'field1' => 'Test_c_actions',
      'field2' => 1.5,
      'field3' => 'A standard text',
    ));
    $this->addResult('Constructor action -> save', true, $o->save(), '===');
    $this->addResult('Constructor action -> gotId', false, is_null($o->get('id')), '===');
    
    $o = new Tester($c, $l, 'load', array(
      'field1' => 'Test_c_actions'
    ));
    $this->addResult('Constructor action -> load -> Loaded', true, $o->Loaded, '===');
    $this->addResult('Constructor action -> load -> Field1', 'Test_c_actions', $o->get('field1'), '===');
    $this->addResult('Constructor action -> load -> Field2', 1.5, $o->get('field2'), '===');
    $this->addResult('Constructor action -> load -> Field3', 'A standard text', $o->get('field3'), '===');
    
    $this->addMessage('Reset and new datasets');
    $o->reset();
    $o->qset(array(
      'field1' => 'Test_qset',
      'field2' => 2,
      'field3' => 'A standard text',
    ));
    $this->addResult('qset -> save', true, $o->save(), '===');
    $this->addResult('qset -> gotId', false, is_null($o->get('id')), '===');
    $this->addResult('qset -> load', true, $o->load(array('field1' => 'Test_qset')), '===');
    $this->addResult('qset -> load -> Loaded', true, $o->Loaded, '===');
    $this->addResult('qset -> load -> Field1', 'Test_qset', $o->get('field1'), '===');
    $this->addResult('qset -> load -> Field2', floatval(2), $o->get('field2'), '===');
    $this->addResult('qset -> load -> Field3', 'A standard text', $o->get('field3'), '===');
    
    $o->reset();
    $o->set('field1', 'Test_set');
    $o->set('field2', 3.5);
    $o->set('field3', 'A standard text');
    $this->addResult('set -> save', true, $o->save(), '===');
    $this->addResult('set -> gotId', false, is_null($o->get('id')), '===');
    $id = $o->get('id');
    $this->addResult('set -> load', true, $o->load(array('field1' => 'Test_set')), '===');
    $this->addResult('set -> load -> Loaded', true, $o->Loaded, '===');
    $this->addResult('set -> load -> Field1', 'Test_set', $o->get('field1'), '===');
    $this->addResult('set -> load -> Field2', 3.5, $o->get('field2'), '===');
    $this->addResult('set -> load -> Field3', 'A standard text', $o->get('field3'), '===');
    
    $this->addMessage('Load, change and save');
    $this->addResult('load by id', true, $o->load(array('id' => $id)), '===');
    $this->addResult('load by id -> Loaded', true, $o->Loaded, '===');
    $this->addResult('load by id -> Field1', 'Test_set', $o->get('field1'), '===');
    $this->addResult('load by id -> Field2', 3.5, $o->get('field2'), '===');
    $this->addResult('load by id -> Field3', 'A standard text', $o->get('field3'), '===');
    $o->set('field3', 'Another text');
    $this->addResult('change -> save', true, $o->save(), '===');
    $this->addResult('change -> load', true, $o->load(array('field1' => 'Test_set')), '===');
    $this->addResult('change -> load -> Loaded', true, $o->Loaded, '===');
    $this->addResult('change -> load -> Field1', 'Test_set', $o->get('field1'), '===');
    $this->addResult('change -> load -> Field2', 3.5, $o->get('field2'), '===');
    $this->addResult('change -> load -> Field3', 'Another text', $o->get('field3'), '===');
    
    $this->addResult('load by field1', true, $o->load(array('field1' => 'Test_qset')), '===');
    $this->addResult('load by field1 -> load -> Loaded', true, $o->Loaded, '===');
    $this->addResult('load by field1 -> load -> Field1', 'Test_qset', $o->get('field1'), '===');
    $this->addResult('load by field1 -> load -> Field2', floatval(2), $o->get('field2'), '===');
    $this->addResult('load by field1 -> load -> Field3', 'A standard text', $o->get('field3'), '===');
    $o->set('field1', 'Test_change');
    $this->addResult('change -> save', true, $o->save(), '===');
    $this->addResult('change -> load', true, $o->load(array('field1' => 'Test_change')), '===');
    $this->addResult('change -> load -> Loaded', true, $o->Loaded, '===');
    $this->addResult('change -> load -> Field1', 'Test_change', $o->get('field1'), '===');
    $this->addResult('change -> load -> Field2', floatval(2), $o->get('field2'), '===');
    $this->addResult('change -> load -> Field3', 'A standard text', $o->get('field3'), '===');
    
    $this->addResult('load by field1 and field2', true, $o->load(array(
      'field1' => 'Test_c_actions',
      'field2' => 1.5
    )), '===');
    $this->addResult('load by field1 and field2 -> load -> Loaded', true, $o->Loaded, '===');
    $this->addResult('load by field1 and field2 -> load -> Field1', 'Test_c_actions', $o->get('field1'), '===');
    $this->addResult('load by field1 and field2 -> load -> Field2', 1.5, $o->get('field2'), '===');
    $this->addResult('load by field1 and field2 -> load -> Field3', 'A standard text', $o->get('field3'), '===');
    $o->qset(array(
      'field1' => 'Test_change2',
      'field2' => 3.7
    ));
    $this->addResult('change -> save', true, $o->save(), '===');
    $this->addResult('change -> load', true, $o->load(array(
      'field1' => 'Test_change2',
      'field2' => 3.7
    )), '===');
    $this->addResult('change -> load -> Loaded', true, $o->Loaded, '===');
    $this->addResult('change -> load -> Field1', 'Test_change2', $o->get('field1'), '===');
    $this->addResult('change -> load -> Field2', 3.7, $o->get('field2'), '===');
    $this->addResult('change -> load -> Field3', 'A standard text', $o->get('field3'), '===');
    
    $this->addMessage('Validation and filter');
    $o->reset();
    $o->qset(array(
      'field1' => '',
      'field2' => 'ab'
    ));
    $chk = $o->validate();
    $this->addResult('Validation not true', true, $chk, '!==');
    $this->addResult('Validation is_array', true, is_array($chk), '===');
    $this->addResult('Validation array_count', 3, count($chk), '===');
    $o->set('field1', '\'Cause filter\'');
    $chk = $o->validate();
    $this->addResult('Validation not true', true, $chk, '!==');
    $this->addResult('Validation is_array', true, is_array($chk), '===');
    $this->addResult('Validation array_count', 2, count($chk), '===');
    $o->set('field2', 3.4);
    $chk = $o->validate();
    $this->addResult('Validation not true', true, $chk, '!==');
    $this->addResult('Validation is_array', true, is_array($chk), '===');
    $this->addResult('Validation array_count', 1, count($chk), '===');
    $o->set('field3', '<b>Cause filter</b>');
    $chk = $o->validate();
    $this->addResult('Validation true', true, $chk, '===');
    $this->addResult('change -> save', true, $o->save(), '===');
    $id = $o->get('id');
    $this->addResult('load by id', true, $o->load(array('id' => $id)), '===');
    $this->addResult('Loaded?', true, $o->Loaded, '===');
    $this->addResult('filter for field1', '\'Cause filter\'', $o->get('field1'), '===');
    $this->addResult('(no) filter for field2', 3.4, $o->get('field2'), '===');
    $this->addResult('filter for field3', 'Cause filter', $o->get('field3'), '===');
    
    $this->addMessage('Remove');
    $this->addResult('load by field', true, $o->load(array('field1' => 'Test_change2')), '===');
    $this->addResult('remove', true, $o->remove(), '===');
    $this->addResult('check', false, $o->load(array('field1' => 'Test_change2')), '===');
    
    $this->addResult('remove by key (without loading)', true, $o->remove($id), '===');
    $this->addResult('check', false, $o->load(array('id' => $id)), '===');
  }
  
  /**
   * Cleanup
   */
  public function cleanup()
  {
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      return;
    }
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('DB selection failed. Test stopped.');
      return;
    }
    $r = mysql_query('TRUNCATE '.$this->_table);
    if (!$r) {
      $this->addMessage('Table could not be truncated.');
      return;
    }
    
    $this->addMessage('Successfully truncated table.');
  }
  
  
}

$t = new TestSimpleDatexS();
$t->start('SimpleDatexS');
